package com.loozb.thread2;

/**
 *
 * 求和
 * @author 龙召碧
 * @create 2018-01-30 17:17
 **/
public class AccountingVol implements Runnable {
    static AccountingVol instance = new AccountingVol();

    static volatile int i = 0;

    public static synchronized void increase() {
        i ++;
    }
    @Override
    public void run() {
        for (int j = 0; j < 10000000;j ++) {
            increase();
        }
    }

    /**
     * 如果使用new AccountingVol()，则上锁需要用静态static
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new AccountingVol());
        Thread t2 = new Thread(new AccountingVol());
        t1.start();t2.start();
        t1.join();t2.join();
        System.out.println(i);
    }
}
